<?php
	class CustomFieldDisplayHelper extends AppHelper
	{
		var $helpers = array("Html");
        const TEMPLATE_DIV_WRAPPED = 1;
        const TEMPLATE_SIMPLE = 2;
		function showField(GigaFieldProperties $FieldPropertyObject,$field,$record,$template = self::TEMPLATE_DIV_WRAPPED,$showEmptyValues = false)
		{
			$type = $FieldPropertyObject->getType($field);
			$return = "";
			if($type == SCHEMA_TYPE_VIDEO)
			{
				$return = "";
				if(isset($record[$field]))
				{
					$videoName = $record[$field];
					$return = $this->showVideo($FieldPropertyObject->getPropertyValue($field, 'label'), $videoName);
					
				}
			}else
			{
                if($template == self::TEMPLATE_DIV_WRAPPED)
				{
                  $return = $this->fieldValueDiv($FieldPropertyObject,$field,$record,$showEmptyValues);
                }else
                {
                  $return = $this->getShowValue($FieldPropertyObject,$field,$record,$showEmptyValues);
                }
            }
			return $return;
		}
		
		function showVideo($label,$videoName)
		{
			$filePath = WWW_ROOT.DS.'uploads'.DS.'videos'.DS.$videoName;
            if(!strlen($videoName) || !file_exists($filePath))
            {
              return null;
            }
			$this->initHelper("OsPlayer");
            $previewImage = '/uploads/videos/'.str_replace('.flv', '.jpg', $videoName);
			$video =  $this->OsPlayer->
					flvstring('/uploads/videos/'.$videoName,$filePath,array('previewImage'=>$previewImage,'width'=>300,'height'=>200,'autoload'=>true));
			$video = '<div class="custom_field">'.$video.'</div>';
			return $video;
		}
        
        function showVideoInThickbox($videoName,$label)
        {
            //thickbox is not able to show properly because js present in video html.
            $video = $this->showVideo(null, $videoName);
            if($video)
            {
              $this->initHelper("Effects.Thickbox");
              $this->Thickbox->reset();
              $id = "video_div_".rand(1,100);
              $this->Thickbox->options = array("previewContent"=>$label,'type'=>'inline','mainContent'=>$video,'height'=>"400px",'width'=>"400px",'id'=>$id);
              $video = $this->Thickbox->output('a');
              
            }
            return $video;
        }
		protected function fieldValueDiv(GigaFieldProperties $FieldPropertyObject,$field,$record,$showEmptyValues = false)
		{
            $value = $this->getShowValue($FieldPropertyObject, $field, $record,$showEmptyValues);
            if($value !== false)
            {
              $label = $FieldPropertyObject->getPropertyValue($field, 'label');
              $div =<<<DIV
              <div class="custom_field">
                  <b>:label</b>
                  <div class="field-value">
                      :value
                  </div>
                  <div class="clear"></div>
              </div>
DIV;
             $value = String::insert($div,array("label"=>$label,'value'=>$value)); 
            }

			return $value;
		}
        
        function getShowValue(GigaFieldProperties $FieldPropertyObject,$field,$record,$showEmptyValues = false)
        {
          	if($FieldPropertyObject->getType($field) == "url" && @strlen($record[$field]))
			{
				$otherInformation = $FieldPropertyObject->getPropertyValue($field,'other_information');
				if(!$otherInformation)
				{
					$otherInformation = array();
				}
				$anchorText = isset($otherInformation['anchor_text']) && strlen($otherInformation['anchor_text']) ? $otherInformation['anchor_text'] : null; 
				$url = $record[$field];
				if(preg_match("/http(s?)\:\/\/.+/",$url) == false)
				{
					$url = "http://".$url;
				}
				if(isset($otherInformation['mask_url']) && $otherInformation['mask_url'])
				{
					$url = $this->Html->url(array("controller"=>"outgoing",'action'=>"url",'Listing',$record['id'],'?'=>array("el"=>base64_encode($url))));
					$url = cr("cmsConfig.Backend.siteUrl").$url;
				}
				if(strlen($anchorText) == false)
				{
					$anchorText = $url;
				}
				$value = '<a href="'.$url.'" rel="nofollow" class="outlink" target="__blank">'.$anchorText.'</a>';
			}else
			{
				$value = $FieldPropertyObject->getShowValue($field, $record);
			}
			if(!strlen($value) )
			{
				$value = false;
                if($showEmptyValues)
                {
                  $value = __("N/A");
                }
			}
            return $value;
        }
	}
?>